package com.bianxianmao.myinfo.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.bianxianmao.MainActivity;
import com.bianxianmao.R;
import com.bianxianmao.utils.Constants;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * Created by Administrator on 2017/7/8 0008.
 * 登录
 */

public class LoginActivity extends Activity implements View.OnClickListener {
    private EditText etPhone;
    private EditText etPassword;
    private Button btnLogin;
    private TextView forgetPwd;
    private TextView regitNewuser;
    private TextView backtoHome;
    private TextView wechat;
    private TextView qq;
    // 微信登录
    private static IWXAPI WXapi;
    private String phone;
    private String pwd;
    private String WX_APP_ID = "wx13efc7ee93ee3569";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        initData();
    }

    public void initView() {
        etPhone = (EditText) findViewById(R.id.et_phone);
        etPassword = (EditText) findViewById(R.id.et_password);

        phone = etPhone.getText().toString();
        pwd = etPassword.getText().toString();
        btnLogin = (Button) findViewById(R.id.btn_login);
        forgetPwd = (TextView) findViewById(R.id.tv_forget_password);
        regitNewuser = (TextView) findViewById(R.id.tv_regist_newuser);
        backtoHome = (TextView) findViewById(R.id.tv_backto_homepage);
        wechat = (TextView) findViewById(R.id.tv_wechat);
        qq = (TextView) findViewById(R.id.tv_qq);
    }

    public void initData() {
        btnLogin.setOnClickListener(this);
        forgetPwd.setOnClickListener(this);
        regitNewuser.setOnClickListener(this);
        backtoHome.setOnClickListener(this);
        wechat.setOnClickListener(this);
        String phone = etPhone.getText().toString();
        String pwd = etPassword.getText().toString();
        if (TextUtils.isEmpty(phone) || (TextUtils.isEmpty(pwd))) {
            Toast.makeText(LoginActivity.this, "请登录", Toast.LENGTH_SHORT).show();
        }
        if (phone != null && pwd != null) {
            btnLogin.setBackgroundResource(R.color.buttonColor_selected);
        } else {
            btnLogin.setBackgroundResource(R.color.buttonColor_unselected);
        }
    }

    //手机号登陆
    private void login() {
        String url = Constants.LOGINURL + "?appkey=" + "mobile=" + phone + "&passWord=" + pwd;
        new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.i("123", "onResponse: +++" + response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
    }

    /**
     * 登录微信
     */
    private void wxLogin() {
        WXapi = WXAPIFactory.createWXAPI(this, WX_APP_ID, true);
        WXapi.registerApp(WX_APP_ID);
        SendAuth.Req req = new SendAuth.Req();
        req.scope = "snsapi_userinfo";
        req.state = "wechat_sdk_demo";
        WXapi.sendReq(req);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.btn_login://登录
                btnLogin.setClickable(false);
                btnLogin.setBackgroundResource(000000);

                String phone = etPhone.getText().toString();
                String pwd = etPassword.getText().toString();
                if (!TextUtils.isEmpty(phone) && !TextUtils.isEmpty(pwd)) {
                    btnLogin.setBackgroundResource(R.drawable.btn_regist_bg);
                    btnLogin.setClickable(true);
                } else {
                    btnLogin.setBackgroundResource(R.drawable.btn_regist_bg_gray);
                    btnLogin.setClickable(false);
                }
                break;
            case R.id.tv_forget_password://忘记密码
                startActivity(new Intent(LoginActivity.this, FindpasswordActivity.class));
                break;
            case R.id.tv_regist_newuser://注册新用户
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
                break;
            case R.id.tv_backto_homepage://返回首页
                startActivity(new Intent(LoginActivity.this, MainActivity.class));
                break;
            case R.id.tv_wechat://微信登录
                wxLogin();

                break;
            //  case R.id.tv_qq:
            //    break;
            default:
                break;
        }
    }


}
